﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crystal.Mapping;
using Microsoft.Xna.Framework;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Crystal.Structure
{
    [Serializable]
    public sealed class LevelSerializableDataStructure
    {
        //Fields
        public LevelMap Levelmap { get; set; }

        //ctor
        public LevelSerializableDataStructure()
        {

        }

               
        //Methods
        /// <summary>
        /// Serialized the data into the File
        /// </summary>
        /// <param name="file">The File</param>
        private static bool Serialize(string file, LevelSerializableDataStructure ds, out Exception e)
        {
            try
            {
                Stream s = File.Open(file, FileMode.OpenOrCreate);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(s, ds);
                s.Close();

                e = null;
                return true;
            }
            catch (Exception ex)
            {
                e = ex;
                return false;
            }

        }

        /// <summary>
        /// Deserializes the data stream and saves it to this object.
        /// </summary>
        /// <param name="file">The File</param>
        /// <returns>true: sucess, false, fail</returns>
        public static LevelSerializableDataStructure Deserialize(string file, out Exception e)
        {
            try
            {
                Stream s = File.Open(file, FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                LevelSerializableDataStructure temp = (LevelSerializableDataStructure)formatter.Deserialize(s);

                e = null;
                return temp;
            }
            catch (Exception ex)
            {
                e = ex;
                return null;
            }
        }
    }
}
